/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

/**
 * @file Preview plugin.
 */

(function()
{
	var previewCmd =
	{
		modes : { wysiwyg:1, source:1 },
		canUndo : false,
		exec : function( editor )
		{ 
      var sHTML,
				config = editor.config,
				baseTag = config.baseHref ? '<base href="' + config.baseHref + '"/>' : '',
				isCustomDomain = CKEDITOR.env.isCustomDomain();

			if ( config.fullPage )
			{
				sHTML = editor.getData()
						.replace( /<head>/, '$&' + baseTag )
						.replace( /[^>]*(?=<\/title>)/, editor.lang.preview );
			}
			else
			{
				var bodyHtml = '<body ',
						body = editor.document && editor.document.getBody();

				if ( body )
				{
					if ( body.getAttribute( 'id' ) )
						bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
					if ( body.getAttribute( 'class' ) )
						bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
				}

				bodyHtml += '>';

				sHTML =
					editor.config.docType +
					'<html dir="' + editor.config.contentsLangDirection + '">' +
					'<head>' +
					baseTag +
					'<title>' + editor.lang.preview + '</title>' +
					CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
					'</head>' + bodyHtml +
					editor.getData() +
					'</body></html>';
			}

			var iWidth	= 640,	// 800 * 0.8,
				iHeight	= 420,	// 600 * 0.7,
				iLeft	= 80;	// (800 - 0.8 * 800) /2 = 800 * 0.1.
			try
			{
				var screen = window.screen;
				iWidth = Math.round( screen.width * 0.8 );
				iHeight = Math.round( screen.height * 0.7 );
				iLeft = Math.round( screen.width * 0.1 );
			}
			catch ( e ){}
      
      editor.formFillerHtmlFiller = function(data){
        return '<html><head></head><body>' + data + '</body></html>';
      }
      
      //sHTML = sHTML.replace('</body>', '<applet id="PDFApplet" name="PDFApplet" width=0 height=0 code="PDFPrinter" codebase="pdfApplet" archive="pdfPrinter.jar,core-renderer.jar,core-renderer-minimal.jar,iText-2.0.8.jar,javaws.jar,plugin.jar,xml-apis-xerces-2.9.1.jar"><param name="jnlp_href" value="pdfApplet.jnlp"></applet><input type="button" value="Print File" onClick=" alert(CKEDITOR.pdfApplet);if(CKEDITOR.pdfApplet){CKEDITOR.pdfApplet.printPDF(\'<html><head></head><body>sdsd</body></html>\');}"/></body>')
      var original = sHTML;
      sHTML = sHTML.replace('<html>', '<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">');
      sHTML = sHTML.replace('<head>', '<head><script src="' + CKEDITOR.plugins.getPath('formFiller') + 'scripts/formFiller.js"></script>');
      sHTML = sHTML.replace('</body>', '<input type="button" value="Print File" onClick="removeTextarea(); removeInput(); if(window.opener.CKEDITOR.pdfApplet){window.opener.CKEDITOR.printPDF(window.opener.CKEDITOR.instances.' + editor.name + '.formFillerHtmlFiller(document.body.innerHTML));}" /></body>'); //

      var oWindow = window.open( '', null, 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' +
				iWidth + ',height=' + iHeight + ',left=' + iLeft );
      
      if ( !isCustomDomain )
			{  
				oWindow.document.open();
				oWindow.document.write( sHTML );
				
				oWindow.document.close();
			}
		}
    }

	var pluginName = 'formFiller';

	// Register a plugin named "preview".
	CKEDITOR.plugins.add( pluginName,
	{
		init : function( editor )
		{
			editor.addCommand( pluginName, previewCmd );
			editor.ui.addButton( 'Form Filler',
				{
					label : editor.lang.preview,
					command : pluginName,
					icon: CKEDITOR.plugins.getPath('formFiller') + 'images/formFiller.png'
				});
		}
	});
})();
